package br.ufc.si.managedBean;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.persistence.PersistenceException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import modelo.Imovel;
import modelo.Usuario;
import dao.ImovelDao;

@ManagedBean(name = "cadastroImovelBean")
public class CadastroImovelBean {
	private Imovel imovel;
	
	public CadastroImovelBean(){
		imovel = new Imovel();
	}

	private String msg;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Imovel getImovel() {
		return imovel;
	}

	public void setImovel(Imovel imovel) {
		this.imovel = imovel;
	}

	public String paginaImovel() {

		return "/imovel/cadastrar_imovel";
	}

	public String cadastrar() throws IOException, ClassNotFoundException {
		ImovelDao imovelDao = new ImovelDao();
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		Usuario usuario = new Usuario();
		usuario.setEmail((String) session.getAttribute("email"));
		imovel.setUsuario(usuario);
		try {
			imovelDao.save(imovel);
			imovel = new Imovel();

			msg = "Imóvel cadastrado com sucesso";

			return "/locador/indexLocador?faces-redirect=true";

		} catch (PersistenceException ex) {
			ex.printStackTrace();
			msg = "O imóvel já está cadastrado";
			return "/locador/indexLocador?faces-redirect=true";

		}
	}

	public String cancelar() {
		return "/locador/indexLocador";
	}

	public String atualizar() throws ClassNotFoundException {
		ImovelDao imovelDao = new ImovelDao();
		
		try {
			imovelDao.update(imovel);
			msg = "Imóvel atualizado";

			return "/locador/indexLocador";
		} catch (PersistenceException ex) {
			ex.printStackTrace();
			msg = "Imóvel não atualizado";
			return "/locador/indexLocador";
		}

	}

	public String visualizar() {
		ImovelDao imovelDao = new ImovelDao();
		
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		try {
			imovel = imovelDao.find(Integer.parseInt(request
					.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return "/imovel/visualizar_imovel";
	}

	public boolean getMostraTabela() {
		ImovelDao imovelDao = new ImovelDao();
		return imovelDao.getLista().size() > 0;
	}

	public String editar() {
		ImovelDao imovelDao = new ImovelDao();
		
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		try {
			imovel = imovelDao.find(Integer.parseInt(request
					.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		return "/imovel/editar_imovel";
	}

	public String excluir() {
		ImovelDao imovelDao = new ImovelDao();
		
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		Imovel imovel = imovelDao.find(Integer.parseInt(request.getParameter("id")));
		imovelDao.delete(imovel);

		return "/locador/indexLocador?faces-redirect=true";
	}

	public String adicionarFotos() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();

		HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
				.getExternalContext().getSession(true);
		session.setAttribute("idImovel", request.getParameter("id"));

		return "/imovel/adicionar_fotos";
	}

	public ImovelDao getImovelDao(){
		return new ImovelDao();
	}
}
